/**
 * Copyright (C) 2008 Wideplay Interactive.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.neodatis.warp;

import org.neodatis.odb.ODBServer;

import com.google.inject.Provider;
import com.wideplay.warp.persist.PersistenceService;
import net.jcip.annotations.GuardedBy;
import net.jcip.annotations.ThreadSafe;

/**
 * @author Jeffrey Chung (jeffreymchung@gmail.com)
 * @author Dhanji R. Prasanna (dhanji@gmail.com)
 * @author Robbie Vanbrabant
 */
@ThreadSafe
class NeodatisPersistenceService extends PersistenceService {
    private final Provider<ODBServer> odbServerProvider;
    @GuardedBy("this")
    private State state = State.FRESH;

    public NeodatisPersistenceService(Provider<ODBServer> odbServerProvider) {
        this.odbServerProvider = odbServerProvider;
    }

    public synchronized void start() {
        // Because we implement the provider using lazy loading
        // we do not need to check for the state here. It will
        // simply return the closed EntityManagerFactory
        odbServerProvider.get();
        this.state = State.STARTED;
    }

    public synchronized void shutdown() {
        // Not sure if we need the check, but for now
        // it's better to be safe than sorry.
        if (State.STARTED == this.state) {
            odbServerProvider.get().close();
            this.state = State.STOPPED;
        }
    }

    private static enum State {
        FRESH, STARTED, STOPPED
    }
}
